import java.util.Arrays;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-05-15
 * Time：08:30
 */
public class MyStack {
    private int[] elem;
    private int usedSize;

    public MyStack() {
        this.elem = new int[2];
    }

    public boolean empty() {
        return usedSize == 0;
    }

    private boolean isFull() {
        return usedSize == elem.length;
    }
    public void push(int val) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
        elem[usedSize++] = val;
    }

    public int pop() {
        if (empty()) {
            throw new RuntimeException("栈为空，出栈失败");
        }
        int ret = elem[usedSize-1];
        usedSize--;
        return ret;
    }

    public int peek() {
        if (empty()) {
            throw new RuntimeException("栈为空！");
        }
        return elem[usedSize - 1];
    }

    public int getUsedSize() {
        return usedSize;
    }
}
